/*
 * Sylvain BERTRAND <digital.ragnarok@gmail.com>
 * code protected by GNU GPL v3
 */
#include <X11/Xlib.h>
#include <glib.h>
#include <libxml/xinclude.h>
#include "../../toolkit/keyboard.h"
#include "../../toolkit/xml.h"
#include "../misc.h"
#include "../geom.h"
#include "../frame.h"
#include "../actions.h"
#include "../window.h"
#include "../stacking.h"
#include "../client.h"
struct options {
	enum wm_direction dir;
};

static gpointer setup_func(xmlNodePtr node)
{
	xmlNodePtr n;
	struct options *o;
	o = g_slice_new0(struct options);
	o->dir = WM_DIRECTION_NORTH;
	if((n = t_xml_find_node(node, "direction"))) {
		gchar *s = t_xml_node_string(n);
		if(!g_ascii_strcasecmp(s, "north") || !g_ascii_strcasecmp(s, "up"))
			o->dir = WM_DIRECTION_NORTH;
		else if(!g_ascii_strcasecmp(s, "south") || !g_ascii_strcasecmp(s, "down"))
			o->dir = WM_DIRECTION_SOUTH;
		else if(!g_ascii_strcasecmp(s, "west") || !g_ascii_strcasecmp(s, "left"))
			o->dir = WM_DIRECTION_WEST;
		else if(!g_ascii_strcasecmp(s, "east") || !g_ascii_strcasecmp(s, "right"))
			o->dir = WM_DIRECTION_EAST;
		g_free(s);
	}
	return o;
}

static void free_func(gpointer o)
{
	g_slice_free(struct options, o);
}

static gboolean run_func(struct wm_actions_data *data, gpointer options)
{
	struct options *o = options;
	if(data->client) {
		gint x, y;
		client_find_move_directional(data->client, o->dir, &x, &y);
		if(x != data->client->area.x || y != data->client->area.y) {
			actions_client_move(data, TRUE);
			client_move(data->client, x, y);
			actions_client_move(data, FALSE);
		}
	}
	return FALSE;
}

static gpointer setup_north_func(xmlNodePtr node)
{
	struct options *o = g_slice_new0(struct options);
	(void)node;
	o->dir = WM_DIRECTION_NORTH;
	return o;
}

static gpointer setup_south_func(xmlNodePtr node)
{
	struct options *o = g_slice_new0(struct options);
	(void)node;
	o->dir = WM_DIRECTION_SOUTH;
	return o;
}

static gpointer setup_east_func(xmlNodePtr node)
{
	struct options *o = g_slice_new0(struct options);
	(void)node;
	o->dir = WM_DIRECTION_EAST;
	return o;
}

static gpointer setup_west_func(xmlNodePtr node)
{
	struct options *o = g_slice_new0(struct options);
	(void)node;
	o->dir = WM_DIRECTION_WEST;
	return o;
}

void action_movetoedge_startup(void)
{
	actions_register("MoveToEdge", setup_func, free_func, run_func);
	actions_register("MoveToEdgeNorth", setup_north_func, free_func, run_func);
	actions_register("MoveToEdgeSouth", setup_south_func, free_func, run_func);
	actions_register("MoveToEdgeEast", setup_east_func, free_func, run_func);
	actions_register("MoveToEdgeWest", setup_west_func, free_func, run_func);
}
